package com.example.wishstar.view.fragment;

import static java.lang.Thread.sleep;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.wishstar.MyApplication;
import com.example.wishstar.R;
import com.example.wishstar.databinding.FragmentMemoryBinding;
import com.example.wishstar.model.Memory;
import com.example.wishstar.view.activity.AddMemoryActivity;
import com.example.wishstar.view.adpter.MemoryAdapter;
import com.example.wishstar.viewmodel.MemoryViewModel;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;


public class MemoryFragment extends Fragment implements MemoryAdapter.OnMemoryItemClickListener{

    private String TAG = "MemoryFragment";
    private MemoryViewModel memoryViewModel;
    private FragmentMemoryBinding fragmentMemoryBinding;
    private SwipeRefreshLayout swipeRefresh;

    private LinearLayoutManager memoryManager;
    private MemoryAdapter memoryAdapter;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        memoryViewModel = new ViewModelProvider(this).get(MemoryViewModel.class);
        fragmentMemoryBinding = FragmentMemoryBinding.inflate(inflater, container, false);
        View root = fragmentMemoryBinding.getRoot();

        memoryManager = new LinearLayoutManager(getContext());
        fragmentMemoryBinding.memoryRecyclerview.setLayoutManager(memoryManager);

        memoryViewModel.getMemory().observe(getActivity(), new Observer<List<Memory>>() {
            @Override
            public void onChanged(List<Memory> memories) {
                if(memories.size() >= 0) {
                    Collections.sort(memories, new Comparator<Memory>() {
                        @Override
                        public int compare(Memory o1, Memory o2) {
                            int type = o1.type - o2.type;
                            if (type == 0) {
                                int dayNum = o1.dayNum - o2.dayNum;
                                if (o1.type == 2) {
                                    return dayNum;
                                }
                                else {
                                    return -dayNum;
                                }
                            }
                            else {
                                return -type;
                            }
                        }
                    });
                    Log.e(TAG, "============================>");
                    for (int i = 0; i < memories.size(); ++i) {
                        Log.e(TAG, memories.get(i).toString());
                    }
                    memoryAdapter = new MemoryAdapter(memories, getContext());
                    memoryAdapter.setOnMemoryItemClickListener(MemoryFragment.this);
                    fragmentMemoryBinding.memoryRecyclerview.setAdapter(memoryAdapter);
                }
            }
        });

        swipeRefresh = fragmentMemoryBinding.memorySwipeRefresh;
        swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                memoryViewModel.refresh();
                swipeRefresh.setRefreshing(false);
            }
        });

        TextView title = root.findViewById(R.id.add_title);
        title.setText(getContext().getString(R.string.title_memory));

        ImageView addImage = root.findViewById(R.id.memory_title_add_image);

        addImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), AddMemoryActivity.class);
                startActivity(intent);
            }
        });

        return root;
    }

    @Override
    public void onResume() {
        super.onResume();
        memoryViewModel.refresh();
    }

    @Override
    public void onItemClick(Memory memory) {
        memoryViewModel.deleteMemory(memory.memoryId);
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                memoryViewModel.refresh();
            }
        }, 500);//0.5秒后执行Runnable中的run方法
    }
}
